package com.manager.devicemanager.controller;

import com.manager.devicemanager.common.DataSourceNameEnum;
import com.manager.devicemanager.common.Result;
import com.manager.devicemanager.entity.ComponentRunStatus1;
import com.manager.devicemanager.service.IComponentRunStatus1Service;
import com.manager.devicemanager.vo.ComponentStatusRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wpf
 * @since 2025-05-29
 * @ 功率器件状态
 */
@RestController
@RequestMapping("/dpstatus")
public class ComponentRunStatus1Controller {
    @Autowired
    private IComponentRunStatus1Service iComponentRunStatus1Service;
    private static final List<String> PRIORITIES = Arrays.asList("烧毁", "退化", "正常", "断电");
    final static int ROW_LENGTH = 5;
    final static  int GROUP_LENGTH = 12;
    @RequestMapping("/getComponentStatus")
    public Result<String[][]> getComponentStatus(@RequestBody ComponentStatusRequestVO componentStatusRequestVO){

        ComponentRunStatus1 lastData = iComponentRunStatus1Service.getLastData(DataSourceNameEnum.PDBOARDCONFIG.getDataSourceName()+componentStatusRequestVO.getDatasourceindex());
        if (lastData == null) {
            lastData=new ComponentRunStatus1();
        }
        // 将 ComponentStatus 的属性存储到数组中
        String[] statuses = {
                lastData.getS1(), lastData.getS2(), lastData.getS3(), lastData.getS4(),
                lastData.getS5(), lastData.getS6(), lastData.getS7(), lastData.getS8(),
                lastData.getS9(), lastData.getS10(), lastData.getS11(), lastData.getS12(),
                lastData.getS13(), lastData.getS14(),lastData.getS15(), lastData.getS16(),
                lastData.getS17(), lastData.getS18()
        };


        // 初始化行列互换后的二维数组
        String[][] processedStatuses = new String[ROW_LENGTH][statuses.length];

        for (int i = 0; i < statuses.length; i++) {

            String[] rowResult = processString(statuses[i]);
            for (int j = 0; j < ROW_LENGTH; j++) {
                // 行列互换存储数据
                processedStatuses[j][i] = rowResult[j];
            }
        }
        return  Result.success(processedStatuses);
    }





    public static String[] processString(String input) {
        if (input == null) {
            return new String[ROW_LENGTH];
        }
        // 按 : 分割字符串
        input = input.replaceAll("^\"|\"$", "");
        String[] values = input.split(":");
        if (values.length != ROW_LENGTH*GROUP_LENGTH) {
            throw new IllegalArgumentException("数据必须包含 "+ROW_LENGTH*GROUP_LENGTH+" 个以 : 分隔的值");
        }
        String[] result = new String[ROW_LENGTH];
        for (int i = 0; i < ROW_LENGTH; i++) {
            int startIndex = i * GROUP_LENGTH;
            String[] group = Arrays.copyOfRange(values, startIndex, startIndex + GROUP_LENGTH);
            result[i] = getHighestPriorityValue(group);
        }
        return result;
    }
    /**
     * 根据优先级获取数组中优先级最高的值
     * @param group 包含 8 个值的数组
     * @return 优先级最高的值
     */
    private static String getHighestPriorityValue(String[] group) {
        int highestPriorityIndex = Integer.MAX_VALUE;
        for (String value : group) {
            int index = PRIORITIES.indexOf(value);
            if (index != -1 && index < highestPriorityIndex) {
                highestPriorityIndex = index;
            }
        }
        return highestPriorityIndex == Integer.MAX_VALUE ? null : PRIORITIES.get(highestPriorityIndex);
    }

}
